﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ISS.IR.RRNMS.Library.BusinessLogic;
using System.Data;
using ISS.IR.RRNMS.Library;
using System.Globalization;
using System.Windows.Forms;


namespace ISS.IR.RRNMS.Web
{
    public partial class Calendar : System.Web.UI.Page
    {
        ISS.IR.RRNMS.Library.BusinessLogic.Calendar PageCalendar = new Library.BusinessLogic.Calendar();
        private void BindRepeater()
        {
            List<Item> Rooms = PageCalendar.SelectItemsByCategory(5);
            rptRooms.DataSource = Rooms;
            rptRooms.DataBind();

        }
        private void Clear()
        {
            ddlAccount.SelectedIndex = 0;
            ddlRoom.SelectedIndex = 0;
            ddlUser.SelectedIndex = 0;
            ddlSlot.SelectedIndex = 0;
            txtnewDate.Text = "";
            lbMessage.Text = "";

            ddlAccount.DataBind();
            ddlRoom.DataBind();


            ddlUser.DataBind();
            //ddlSlot.DataSource = PageCalendar.SelectListOfAllSlots();
            //ddlSlot.DataTextField = "Value";
            //ddlSlot.DataValueField = "GUID";
            ddlSlot.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlAccount.DataSource = PageCalendar.SelectListOfAllAccounts();
                ddlAccount.DataTextField = "Value";
                ddlAccount.DataValueField = "GUID";

                ddlAccount.DataBind();


                ddlUser.DataSource = PageCalendar.SelectListOfAllUsers();
                ddlUser.DataTextField = "Value";
                ddlUser.DataValueField = "GUID";
                ddlUser.DataBind();

                ddlRoom.DataSource = PageCalendar.SelectListOfAllRooms(5);
                ddlRoom.DataTextField = "Value";
                ddlRoom.DataValueField = "Id";
                ddlRoom.DataBind();

                ddlSlot.DataSource = PageCalendar.SelectListOfAllSlots();
                ddlSlot.DataTextField = "Value";
                ddlSlot.DataValueField = "GUID";
                ddlSlot.DataBind();

                lbMessage.Text = "";

            }

            


        }

        protected void BtSearch_Click(object sender, EventArgs e)
        {
            BindRepeater();
        }

        protected void rptRooms_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {


                List<Item> Rooms = PageCalendar.SelectItemsByCategory(5);

                //((Label)e.Item.FindControl("LbRooms")).Text = LiRoom. 
                //if(e.Item.Controls.Contains("hfRoomId"))


                string strRoomId = (e.Item.FindControl("hfRoomId") as HiddenField).Value;
                int roomId = 0;
                if (!String.IsNullOrEmpty(strRoomId))
                {
                    roomId = Convert.ToInt32(strRoomId);
                }
                GridView gvCalenderEntries = (GridView)e.Item.FindControl("gvCalenderEntries");
                try
                {

                    gvCalenderEntries.DataSource = PageCalendar.SelectCalendarEntitesByDateAndRoom(Convert.ToDateTime(txtDate.Text), roomId);
                    gvCalenderEntries.DataBind();
                    //// (Repeater) e.Item.FindControl("RepeaterVeiw").
                }
                catch (Exception)
                {

                }
            }
        }

        protected void btnNewDate_Click(object sender, EventArgs e)
        {
            Clear();
            if (!String.IsNullOrEmpty(txtDate.Text))
            { txtnewDate.Text = txtDate.Text; }

            programmaticModalPopup_AlreadyRegistered.Show();

            tbNewAppointmant.Rows[4].Style.Add("display", "none");
            tbNewAppointmant.Rows[5].Style.Add("display", "none");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Library.BusinessLogic.Calendar calender = new Library.BusinessLogic.Calendar();
            if (!string.IsNullOrEmpty(ddlRoom.SelectedValue) && !string.IsNullOrEmpty(ddlAccount.SelectedValue) && !string.IsNullOrEmpty(ddlSlot.SelectedValue) && !string.IsNullOrEmpty(txtnewDate.Text) && !string.IsNullOrEmpty(txtnewDate.Text))
            {
                calender = new Library.BusinessLogic.Calendar();

                calender.Date = Convert.ToDateTime(txtnewDate.Text);

                if (rbnRegisteredAccount.Checked)
                {
                    Item AccountTemp = new Item();
                    AccountTemp.GUID = Guid.Parse(ddlAccount.SelectedValue.ToString());
                    calender.Account = AccountTemp;
                }
                else if (rbnNewAccount.Checked)
                {
                    calender.Account = new Item() { GUID = calender.CalendarSaveAccount(txtFristName.Text, txtSecondName.Text, txtThirdName.Text, txtFourthName.Text, txtUNHCR.Text, txtTele.Text) };
                }
                calender.Room = new Item() { Id = Convert.ToInt32(ddlRoom.SelectedValue) };

                calender.Slot = new Slot() { SlotId = Guid.Parse(ddlSlot.SelectedValue.ToString()) };

                calender.User = new Item() { GUID = Guid.Parse(ddlUser.SelectedValue.ToString()) };

                if (calender.SelectSlotsToCheckBeforeInsert(calender))
                {
                    calender.InsertNewAppointmant(calender);
                    txtDate.Text = txtnewDate.Text;
                    BindRepeater();
                    programmaticModalPopup_AlreadyRegistered.Hide();
                }
                else
                {
                    lbMessage.Text = "Room Or User is busy , Please choose another one !!!";
                    programmaticModalPopup_AlreadyRegistered.Show();
                }



            }

        }

        protected void txtnewDate_TextChanged(object sender, EventArgs e)
        {
            Library.BusinessLogic.Calendar calendar = new Library.BusinessLogic.Calendar();
            if (!string.IsNullOrEmpty(ddlRoom.SelectedValue) && !string.IsNullOrEmpty(ddlUser.SelectedValue) && !string.IsNullOrEmpty(ddlAccount.SelectedValue) && !string.IsNullOrEmpty(ddlSlot.SelectedValue) && !string.IsNullOrEmpty(txtnewDate.Text))
            {
                calendar.Date = Convert.ToDateTime(txtnewDate.Text);
                calendar.Room = new Item() { Id = Convert.ToInt32(ddlRoom.SelectedValue) };
                Item UserTemp = new Item();
                UserTemp.GUID = Guid.Parse(ddlUser.SelectedValue.ToString());
                calendar.User = UserTemp;
                //calendar.User = new Item() { GUID = Guid.Parse(ddlUser.SelectedValue.ToString()) };

                List<Guid> Slots = PageCalendar.SelectSlotsToCheck(calendar);

                for (int i = 0; i < ddlSlot.Items.Count; i++)
                {
                    ddlSlot.Items[i].Enabled = true;
                }

                for (int j = 0; j < Slots.Count; j++)
                {
                    ddlSlot.Items.FindByValue(Slots[j].ToString()).Enabled = false;

                }



            }

        }


        protected void ddlRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            Library.BusinessLogic.Calendar calendar = new Library.BusinessLogic.Calendar();
            if (!string.IsNullOrEmpty(ddlRoom.SelectedValue) && !string.IsNullOrEmpty(ddlUser.SelectedValue) && !string.IsNullOrEmpty(ddlAccount.SelectedValue) && !string.IsNullOrEmpty(ddlSlot.SelectedValue) && !string.IsNullOrEmpty(txtnewDate.Text))
            {
                calendar.Date = Convert.ToDateTime(txtnewDate.Text);

                calendar.Room = new Item() { Id = Convert.ToInt32(ddlRoom.SelectedValue) };
                Item UserTemp = new Item();
                UserTemp.GUID = Guid.Parse(ddlUser.SelectedValue.ToString());
                calendar.User = UserTemp;
                //calendar.User = new Item() { GUID = Guid.Parse(ddlUser.SelectedValue.ToString()) };

                List<Guid> Slots = PageCalendar.SelectSlotsToCheck(calendar);

                for (int i = 0; i < ddlSlot.Items.Count; i++)
                {
                    ddlSlot.Items[i].Enabled = true;
                }
                for (int j = 0; j < Slots.Count; j++)
                {

                    ddlSlot.Items.FindByValue(Slots[j].ToString()).Enabled = false;

                }

            }
        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            Library.BusinessLogic.Calendar calendar = new Library.BusinessLogic.Calendar();
            if (!string.IsNullOrEmpty(ddlRoom.SelectedValue) && !string.IsNullOrEmpty(ddlUser.SelectedValue) && !string.IsNullOrEmpty(ddlAccount.SelectedValue) && !string.IsNullOrEmpty(ddlSlot.SelectedValue) && !string.IsNullOrEmpty(txtnewDate.Text))
            {
                calendar.Date = Convert.ToDateTime(txtnewDate.Text);

                calendar.Room = new Item() { Id = Convert.ToInt32(ddlRoom.SelectedValue) };
                Item UserTemp = new Item();
                UserTemp.GUID = Guid.Parse(ddlUser.SelectedValue.ToString());
                calendar.User = UserTemp;
                //calendar.User = new Item() { GUID = Guid.Parse(ddlUser.SelectedValue.ToString()) };

                List<Guid> Slots = PageCalendar.SelectSlotsToCheck(calendar);

                for (int i = 0; i < ddlSlot.Items.Count; i++)
                {
                    ddlSlot.Items[i].Enabled = true;
                }
                for (int j = 0; j < Slots.Count; j++)
                {
                    ddlSlot.Items.FindByValue(Slots[j].ToString()).Enabled = false;

                }

            }
        }

        protected void gvCalenderEntries_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //if (e.CommandName == "DeleteAppointmant")
            //{
            //    GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            try
            {
                PageCalendar.Account = new Item() { GUID = Guid.Parse(((HiddenField)((GridViewRow)((GridView)sender).Rows[Convert.ToInt32(e.CommandArgument)]).FindControl("hfAccountId")).Value) };
                PageCalendar.User = new Item() { GUID = Guid.Parse(((HiddenField)((GridViewRow)((GridView)sender).Rows[Convert.ToInt32(e.CommandArgument)]).FindControl("hfUserId")).Value) };
                PageCalendar.Slot = new Slot() { SlotId = Guid.Parse(((HiddenField)((GridViewRow)((GridView)sender).Rows[Convert.ToInt32(e.CommandArgument)]).FindControl("hfSlotId")).Value) };
                PageCalendar.Room = new Item() { Id = Convert.ToInt32(((HiddenField)((RepeaterItem)((GridView)sender).Parent).FindControl("hfRoomId")).Value) };
                PageCalendar.Date = Convert.ToDateTime(txtDate.Text);

                PageCalendar.DeleteAppointmant(PageCalendar);
                BindRepeater();
            }
            catch (Exception)
            { }
            //}
        }

    }
}

